/**
 * @author 徐楠
 * @date 2022/5/4 10:49
 * @version 1.0
 */

package com.xunan.likou;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ComplexMultiplication {
    public static void main(String[] args) {
        String num1 = "1+1i";
        String num2 = "1+1i";
        /*String s = complexNumberMultiply(num1, num2);
        System.out.println(s);*/

        //test01();
        String s = complexNumberMultiply2(num1, num2);
        System.out.println(s);
    }

    public static String complexNumberMultiply(String num1, String num2) {
        String[] split1 = num1.split("\\+|i");
        String[] split2 = num2.split("\\+|i");
        Integer a = Integer.valueOf(split1[0]);
        Integer b = Integer.valueOf(split1[1]);
        Integer c = Integer.valueOf(split2[0]);
        Integer d = Integer.valueOf(split2[1]);
        return (a * c - b * d) + "+" + (a * d + b * c) + "i";
    }

    public static String complexNumberMultiply2(String num1, String num2) {
        String regularExpression = "[^0-9|-]";
        Pattern p = Pattern.compile(regularExpression);
        Matcher matcher1 = p.matcher(num1);
        String trim1 = matcher1.replaceAll(" ").trim();
        Matcher matcher2 = p.matcher(num2);
        String trim2 = matcher2.replaceAll(" ").trim();

        System.out.println(trim1);
        System.out.println(trim2);

        String[] split1 = trim1.split(" ");
        String[] split2 = trim2.split(" ");
        Integer a = Integer.valueOf(split1[0]);
        Integer b = Integer.valueOf(split1[1]);
        Integer c = Integer.valueOf(split2[0]);
        Integer d = Integer.valueOf(split2[1]);
        return (a * c - b * d) + "+" + (a * d + b * c) + "i";
    }

    public static void test01() {
        //String a = "love-212sadasd555..55";
        String a = "-1-1i";
        String regularExpression = "[^0-9|-]";
        Pattern p = Pattern.compile(regularExpression);
        Matcher matcher = p.matcher(a);
        String trim = matcher.replaceAll(" ").trim();
        String[] split = trim.split("-");
        for (String s : split) {
            System.out.println(s);
        }
        System.out.println(trim);
    }
}
